<template>
	<view class="pd16_15">
		<view class="box pd16_15 flex">
			<image class="tuan-buy-l" :src="buydata.img" mode="aspectFill"></image>
			<view class="tuan-buy-r pl15">
				<view v-if="mid>0" class="ft14 ftw600 cl-main">{{buydata.name}}【单集购买】【ID：{{mid}} 】</view>
				<view v-else class="ft14 ftw600 cl-main">{{buydata.title}}</view>
				<view v-if="mid>0" class="mt12 alcenter">
					<text class="ft14 cl-orange">单集解锁只需：{{pricebuy}}</text>
					<!-- <view>
						<text class="ft14 cl-orange">单集会员价：</text>
						<text class="ft16 cl-orange ftw600">{{buydata.vippriced}}</text>
					</view>
					<view>
						<text class="ft14 cl-notice">单集普通价：</text>
						<text class="ft14 cl-notice text-line">{{buydata.priced}}</text>
					</view> -->
				</view>
				<view v-else class="mt12 alcenter">
					<text class="ft14 cl-orange">全集解锁只需：{{pricebuy}}</text>
					<!-- <view>
						<text class="ft14 cl-orange">全集会员价：</text>
						<text class="ft16 cl-orange ftw600">{{buydata.vippriceq}}</text>
					</view>
					<view>
						<text class="ft14 cl-notice">全集普通价：</text>
						<text class="ft14 cl-notice text-line">{{buydata.priceq}}</text>
					</view> -->

				</view>
			</view>
		</view>
		<view class="box mt16 pd16_15">
			<!-- <view class="flex alcenter center">
				<text class="iconfont iconbtn_plus ft32" @click="jianAct" style="color: #5C5E66;"></text>
				<view class="ft16 ftw600 cl-main" style="width: 100rpx; text-align: center;">{{num}}</view>
				<text class="iconfont iconbtn_add ft32"   @click="jiaAct"  :style="{color:tempColor}"></text>
			</view>
			<view class="bd-line mt16 mb16"></view>
			<view class="flex alcenter space">
				<text class="ft14 cl-info2">优惠券</text>
				<view>
					<select-coupon v-model="coupon_id" :coupons="coupons"></select-coupon>
				</view>
			</view>
			<view class="bd-line mt16 mb16"></view>
			<view class="flex alcenter space">
				<text class="ft14 cl-info2">会员优惠</text>
				<text class="ft16 cl-main ftw600">-$20</text>
			</view>
			<view class="bd-line mt16 mb16"></view> -->
			<view class="flex alcenter space">
				<text class="ft14 cl-info2">需支付</text>
				<text class="ft16 cl-main ftw600 cl-orange">{{pricebuy}}</text>
			</view>
		</view>

		<view class="box mt16 pd16_15">
			<view class="ft16 pb16 ftw600 cl-main">支付方式</view>
			<view class="bd-line mt16"></view>
			<radio-group @change="changeType">
				<!-- 			<view class="pt16 pb16 flex alcenter space">
					<view class="flex alcenter">
						<text class="iconfont iconicon_pay_balance ft32 cl-green"></text>
						<text class="ml15 ft14 ftw500 cl-main">微信支付</text>
					</view>
					<view>
						<radio value="weixin" :checked="payType ==  'weixin'" :color="tempColor"  />
					</view>
				</view> -->
				<view class="pt16 pb16 flex alcenter space">
					<view class="flex alcenter">
						<text class="iconfont iconicon_pay_balance ft32 cl-green"></text>
						<text class="ml15 ft14 ftw500 cl-main">GooglePay</text>
					</view>
					<view>
						<radio value="GooglePay" :checked="payType ==  'GooglePay'" :color="tempColor" />
					</view>
				</view>
				<!-- <view class="pt16 pb16 flex alcenter space">
					<view class="flex alcenter">
						<text class="iconfont iconicon_pay_balance ft32 cl-green"></text>
						<text class="ml15 ft14 ftw500 cl-main">信用卡支付</text>
					</view>
					<view>
						<radio value="xinyongka" :checked="payType ==  'xinyongka'" :color="tempColor" />
					</view>
				</view> -->
				<!-- <view class="bd-line"></view> -->
			<!-- 	<view class="pt16 flex alcenter space">
					<view class="flex alcenter">
						<text class="iconfont iconicon_pay_balance ft32" style="color: #FFBD1E;"></text>
						<text class="ml15 ft14 ftw500 cl-main">余额支付</text>
					</view>
					<view>
						<radio value="money" :checked="payType ==  'money'" :disabled="false" :color="tempColor" />
					</view>
				</view> -->
			</radio-group>
		</view>
		<view class="form-footer-h">
			<view class="form-footer form-footer-h">
				<view class="form-footer-main pd10_15">
					<button v-if="mid>0" class="btn-big" @click="okTap" :style="getBtnStyle">支付 ${{pricebuy}}</button>
					<button v-else class="btn-big" @click="okTap" :style="getBtnStyle">支付 {{pricebuy}}</button>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	var googlePay = uni.requireNativePlugin('sn-googlepay5');
	export default {
		data() {
			return {
				payType: 'GooglePay',
				num: 1,
				coupon_id: 0,
				buydata: [],
				buytype: 'GooglePay',
				dataList: [],
				money: 0,
				mid: 0,
				openid: '',
				pricebuy: 0,
				wxlx: 'wxh5',
				vid: '',
				isConnected: false,
				coupons: [{
						coupon_id: 1,
						num: 20
					},
					{
						coupon_id: 2,
						num: 30
					},
					{
						coupon_id: 3,
						num: 40
					},
				],
				selectedProduct: {

				},
			}
		},
		onLoad(option) {
			console.log(option, 'option');
			this.mid=option.mid
			this.vid = option.vid
			console.log(this.vid, 'this.vid');
			googlePay.init({}, (e) => {
				console.log('init', e);
				if (e.code == 0) {
					this.isConnected = true;
					var pId = this.vid;
					googlePay.querySku({
							inapp: [pId]
						},
						(e) => {
							if (e.code == 0) {
								console.log('querySku', e.list);
								this.selectedProduct.allVAalue = e.list
								if (e.list.length > 0) {
									var pd = e.list[0];
									console.log(pd.oneTimePurchaseOfferDetails.formattedPrice, 'pd');
									this.pricebuy = pd.oneTimePurchaseOfferDetails.formattedPrice //价格
								} else {
									console.log('未查询到产品');
								}
							} else {
								//查询失败
								console.log('querySku fail', e);
							}
						}
					);

				} else {
					// 初始化失败
					this.isConnected = false;
				}
			});


			this.buydata = uni.getStorageSync("buydata")
			console.log('0000', this.buydata);
			if (this.mid > 0 && this.buydata.isvip == 2) {
				this.pricebuy = this.buydata.vippriced
			} else if (this.mid > 0 && this.buydata.isvip == 1) {
				this.pricebuy = this.buydata.priced
			} else if (this.mid == 0 && this.buydata.isvip == 2) {
				this.pricebuy = this.buydata.vippriceq
			} else if (this.mid == 0 && this.buydata.isvip == 1) {
				this.pricebuy = this.buydata.priceq
			}
			console.log('this.pricebuy----',this.pricebuy);
			//#ifdef MP-WEIXIN
			this.wxlx = 'wxxcx'
			this.wxlogin()
			//#endif
			//#ifdef H5
			var ua = window.navigator.userAgent.toLowerCase();
			if (ua.match(/MicroMessenger/i) == 'micromessenger') {
				this.wxlx = 'wxgzh';
			}
			//#endif
		},
		onShow() {

			if (uni.getStorageSync("userinfo").token) {
				this.ongrzlTap()
				this.money = uni.getStorageSync("userinfo").money
			} else {
				uni.showModal({
					title: this.$t('play.win'),
					content: this.$t('play.lookdl'),
					showCancel: true,
					confirmText: this.$t('play.dl'),
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/login/login'
							})
						} else if (res.cancel) {
							uni.navigateBack();
						}
					}
				});
			}
		},

		computed: {
			getCouponMoney() {
				if (this.coupons.length > 0) {
					for (var a in this.coupons) {
						if (this.coupons[a].coupon_id == this.coupon_id) {
							return this.coupons[a].num;
						}
					}
				}
				return 0;
			}
		},
		methods: {
			async payxyk(num) {

				let params = {
					money: num
				}
				params.token = uni.getStorageSync("userinfo").token
				let [err, res] = await this.$httpas.get('/api/cz/addOrder', params);
				window.location.href = res.data.data.order.payment_url;
			},
			okTap() {
				var thia = this
				this.dataList = this.buydata;
				console.log(this.dataList, '---');
				this.dataList.sum = 1;
				this.dataList.buytype = this.buytype;
				//https://rw.gzzsw.cn/api/pay/index?token=a69720a4-6c4c-4baf-b269-d5260b821c7a
				let data = this.dataList;
				console.log('==data==', data);
				data.pricebuy = thia.pricebuy
				data.token = uni.getStorageSync("userinfo").token;
				console.log(data.token, 'data.token');
				data.uid = uni.getStorageSync("userinfo").id;
				if (this.mid > 0) {
					data.lx = 2; //1 全章购买 2单集购买
					data.smid = this.mid;
					data.namet = data.name + this.$t('text.danjigoumai') + '【ID：' + this.mid + ' 】';
				} else {
					data.lx = 1; //1 全章购买 2单集购买
					data.smid = 0;
					data.namet = data.name
				}
				data.wxlx = this.wxlx;
				data.openid = this.openid;
				data.info = [];
				/* 	 that.$httpas.post2({
							  	data: {
							  		do: 'pay',
							  		op: 'videopay',
									...data
							  	},
							  	success: res => {
							  		uni.showToast({
							  			title: ''+res,
							  			icon: 'none'
							  		});
							
							  	}
							  })  */
				console.log(thia.dataList.buytype, 'thia.dataList.buytype');
				if (thia.dataList.buytype == 'GooglePay') {
					googlePay.pay({
							productId: this.vid // 产品id
						},
						(e) => {
							console.log('pay result', e);
							// 步骤三：确认订单
							googlePay.consume({
									purchaseToken: e.data[0].original.purchaseToken
								},
								(e) => {
									console.log('consume result', e,data);
									console.log('thia.selectedProduct.allVAalue',thia.selectedProduct.allVAalue);
									thia.$httpas.post2({
										data: {
											do: 'pay',
											op: 'videopay',
											eee: JSON.stringify(e),
											allID: JSON.stringify(thia.selectedProduct.allVAalue),
											...data
										},
										success: res => {
											// console.log('res',res)
											uni.showToast({
												title: this.$t('toast.pay.success'),
												icon: 'none'
											});
											
											  uni.navigateBack();
										}
										
									})
								}
							);

						}
					);
				} 
				// else if (thia.dataList.buytype == '余额') {
				// 	console.log(data, 'data--');
				// 	uni.request({
				// 		url: this.configs.webUrl + '/api/pay/videopay',
				// 		data: data,
				// 		success: resa => {
				// 			console.log(resa, 'resa');
				// 			if (thia.dataList.buytype == '余额') {
				// 				if (resa.data.code == 1) {

				// 					//pages/user/task/task
				// 					uni.showModal({
				// 						title: this.$t('play.wins'),
				// 						content: resa.data.msg,
				// 						showCancel: false,
				// 						success: res => {
				// 							uni.setStorage({
				// 								key: 'isbuy',
				// 								data: 2
				// 							})
				// 							uni.navigateBack();
				// 							// uni.navigateBack({
				// 							// 	delta: 2
				// 							// });
				// 						}
				// 					});
				// 				} else {
				// 					console.log(resa.data, '2');
				// 					if (resa.data.msg == '余额不足') {
				// 						uni.showModal({
				// 							title: this.$t('play.wins'),
				// 							content: resa.data.msg,
				// 							showCancel: true,
				// 							confirmText: this.$t('text.recharge'),
				// 							success: function(res) {
				// 								if (res.confirm) {
				// 									var pricebuy = thia.pricebuy
				// 									uni.navigateTo({
				// 										url: "/pages/client/vipcard/recharge?price=" +
				// 											pricebuy * 1
				// 									})
				// 								} else if (res.cancel) {
				// 									//uni.navigateBack();
			
				// 								}
				// 							}
				// 						});
				// 					} else {
				// 						uni.showModal({
				// 							title: this.$t('play.wins'),
				// 							content: resa.data.msg,
				// 							showCancel: false,
				// 							success: res => {
				// 								uni.navigateBack();
				// 							}
				// 						});
				// 					}
				// 				}
				// 			}
				// 			//thia.okPay(resa)
				// 		},
				// 		fail: (data, code) => {
				// 			console.log('fail' + JSON.stringify(data));
				// 		}
				// 	});
				// }


				// console.log(data, 'thia.pricebuy', thia.pricebuy)
				// uni.request({
				// 	url: this.configs.webUrl+'/api/pay/videopay',
				// 	data: data,
				// 	success: resa =>{
				// 		//console.log(resa.data);
				// 		if(thia.dataList.buytype=='信用卡'){
				// 			thia.payxyk(thia.pricebuy)
				// 			return 1

				// 		}else if(thia.dataList.buytype=='微信'){
				// 			if(this.wxlx=='wxxcx' && resa.data.code==1){
				// 				//console.log(resa.data.code);
				// 				this.wxxcx(resa.data.data);
				// 			}else{
				// 				// #ifdef APP-PLUS

				// 				uni.navigateTo({
				// 					url:'/pages/client/webva?url='+resa.data.data
				// 				})
				// 				//plus.runtime.openURL(res.data.data);
				// 				// #endif  
				// 				// #ifdef H5
				// 				window.open(resa.data.data, "_self")
				// 				// #endif 
				// 			}
				// 		}else if(thia.dataList.buytype=='余额'){
				// 			if(resa.data.code==1){
				// 				//pages/user/task/task
				// 				uni.showModal({
				// 					title: this.$t('play.wins'),
				// 					content: resa.data.msg,
				// 					showCancel: false,
				// 					success: res => {
				// 						uni.setStorage({
				// 							key: 'isbuy',  
				// 							data: 2
				// 						})
				// 						uni.navigateBack();
				// 						// uni.navigateBack({
				// 						// 	delta: 2
				// 						// });
				// 					}
				// 				});
				// 			}else{
				// 				if(resa.data.msg=='余额不足'){
				// 					uni.showModal({
				// 						title: this.$t('play.wins'),
				// 						content: resa.data.msg,
				// 						showCancel: true,
				// 						confirmText: this.$t('text.recharge'),
				// 						success: function (res) {
				// 							if (res.confirm) {
				// 								var pricebuy=thia.pricebuy
				// 								uni.navigateTo({
				// 									url:"/pages/client/vipcard/recharge?price="+pricebuy*1
				// 								})
				// 							} else if (res.cancel) {
				// 								//uni.navigateBack();
				// 							}
				// 						}
				// 					});
				// 				}else{
				// 					uni.showModal({
				// 						title: this.$t('play.wins'),
				// 						content: resa.data.msg,
				// 						showCancel: false,
				// 						success: res => {
				// 							uni.navigateBack();	
				// 						}
				// 					});
				// 				}
				// 			}
				// 		}
				// 		//thia.okPay(resa)
				// 	},
				// 	fail: (data, code) => {
				// 		console.log('fail' + JSON.stringify(data));
				// 	}
				// });
			},
			wxxcx(data) {
				uni.requestPayment({
					"provider": 'wxpay',
					"appId": data.appId,
					"timeStamp": data.timeStamp + '',
					"nonceStr": data.nonceStr,
					"package": data.package,
					"signType": 'MD5',
					"paySign": data.sign,
					success: function(res1) {
						uni.showModal({
							title: this.$t('play.win'),
							content: this.$t('toast.pay.success'),
							showCancel: false,
							confirmText: this.$t('text.qd'),
							success: function(res) {
								if (res.confirm) {
									uni.setStorage({
										key: 'isbuy',
										data: 2
									})
									uni.navigateBack();
								} else if (res.cancel) {

								}
							}
						});
						//uni.showToast({ title: '支付成功',icon:"none" });
						console.log('success:' + JSON.stringify(res1));
					},
					fail: function(err1) {
						uni.showModal({
							title: this.$('toast.pay.fail'),
							content: JSON.stringify(err1),
							showCancel: false,
							confirmText: this.$t('text.qd'),
							success: function(res) {
								if (res.confirm) {
									//uni.navigateBack();
								} else if (res.cancel) {

								}
							}
						});
						//uni.showToast({ title: JSON.stringify(err1),icon:"none" });
						console.log('fail:' + JSON.stringify(err1));
					}
				});
			},
			async ongrzlTap() {
				let data = {};
				data.token = uni.getStorageSync("userinfo").token;
				data.uid = uni.getStorageSync("userinfo").id;
				uni.request({
					url: this.configs.webUrl + '/api/user/index',
					data: data,
					success: res => {
						if (res.data.code == 1) {
							let ionfo = res.data.data
							ionfo.isLogin = true
							this.userinfo = ionfo
							uni.setStorage({ //缓存配置信息
								key: 'userinfo',
								data: res.data.data
							})
							if (res.data.data.avatar) {
								var str = res.data.data.avatar;
								if (str.indexOf("data:image") != -1) {
									this.avatar = '';
								} else {
									if (str.indexOf("http") != -1) {
										this.avatar = res.data.data.avatar;
									} else {
										this.avatar = this.configs.imgUrl + res.data.data.avatar;
									}
								}
							} else {
								this.avatar = '';
							}
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: "none"
							});
						}
					},
					fail: (data, code) => {
						//console.log('fail' + JSON.stringify(data));
					}
				});
			},
			jianAct() {
				if (this.num == 1) return;
				this.num = this.num - 1;
			},
			jiaAct() {
				if (this.num > 99) return; //后面改为库存数量
				this.num = this.num + 1;
			},
			changeType(e) {
				this.payType = e.detail.value;
				if (e.detail.value == 'money') {
					this.buytype = '余额';
				}
				if (e.detail.value == 'weixin') {
					this.buytype = '微信';
				}
			},
			wxlogin() {
				var this_ = this
				uni.login({
					timeout: 10000,
					provider: 'weixin', //如果是uniapp，在这里需要标明平台的类型，支持的参数请查阅uniapp官网的uni.login()文档
					success: (res) => {
						console.log(res);
						//登陆成功的回调
						uni.request({
							url: this.configs.webUrl + '/api/user/getOpenid',
							method: 'GET',
							data: {
								token: uni.getStorageSync("userinfo")
									.token, //你的小程序的secret,  
								code: res.code //wx.login 登录成功后的code  
							},
							success: (cts) => {
								console.log(cts.data.code);
								if (cts.data.code == 1) {
									this_.openid = cts.data.data.openid
								} else {
									uni.showModal({
										title: this.$t('play.win'),
										content: JSON.stringify(cts.data),
										showCancel: true,
										confirmText: this.$t('text.qd'),
										success: function(res) {
											if (res.confirm) {
												uni.navigateBack();
											} else if (res.cancel) {

											}
										}
									});
								}
							},
							fail: (err1) => {
								console.log(err1);
								//失败的回调
							}
						});

					},
					fail: (err) => {
						console.log(err);
						//登陆失败的回调
					}
				})
			}
		}
	}
</script>

<style>
	.tuan-buy-l {
		width: 200rpx;
		height: 150rpx;
		background: #F2F2F2;
	}

	.tuan-buy-r {
		width: calc(100% - 200rpx);
	}
</style>